<template>
  <el-form :model="form" label-width="auto" style="max-width: 600px;background-color: #fff;padding:20px;">
    <el-form-item>
      <div class="filter-open">
        <el-text size="large">防火墙总开关</el-text>
        <el-switch
            v-model="form.IPFilterOpen"
            size="large"
            inline-prompt
            active-text="开启"
            inactive-text="关闭"
        />
      </div>
    </el-form-item>
    <el-divider content-position="center">IP过滤</el-divider>
    <el-form-item label="IP过滤开关">
      <el-switch v-model="form.IPFilterOFF"
                 inline-prompt
                 active-text="开启"
                 inactive-text="关闭"/>
    </el-form-item>
    <el-form-item label="IP白名单">
      <el-input v-model="form.IPWhite" type="textarea"  :rows="4"/>
    </el-form-item>
    <el-form-item label="IP黑名单">
      <el-input v-model="form.IPBlack" type="textarea" :rows="4"/>
    </el-form-item>
    <div class="text-block">
      <p>说明：多个IP以换行分割，黑名单直接拦截。</p>
      <p>配置示例：(0.0.0.0/0及::/0表示包含所有)</p>
      <p>0.0.0.0/0</p>
      <p>::/0</p>
      <p>192.168.0.1</p>
      <p>192.168.1.0/24</p>
    </div>

    <el-divider content-position="center">URL过滤</el-divider>
    <el-form-item label="URL过滤开关">
      <el-switch v-model="form.URLFilterOFF"
                 inline-prompt
                 active-text="开启"
                 inactive-text="关闭"/>
    </el-form-item>
    <el-form-item label="URL过滤白名单">
      <el-input v-model="form.URLWhite" type="textarea" :rows="4"/>
    </el-form-item>
    <el-form-item label="URL过滤黑名单">
      <el-input v-model="form.URLBlack" type="textarea" :rows="4"/>
    </el-form-item>
    <div class="text-block">
      <p>说明：多个URL正则以换行分割，黑名单直接拦截。</p>
      <p>配置正则示例：(.*表示包含所有)</p>
      <p>.*</p>
      <p>https://www.example.com</p>
      <p>https?://[^/]+/api/user/list</p>
    </div>

    <el-divider content-position="center">UserAgent过滤</el-divider>
    <el-form-item label="UserAgent过滤开关">
      <el-switch v-model="form.UserAgentFilterOFF"
                 inline-prompt
                 active-text="开启"
                 inactive-text="关闭"/>
    </el-form-item>
    <el-form-item label="UserAgent过滤白名单">
      <el-input v-model="form.UserAgentWhite" type="textarea" :rows="4"/>
    </el-form-item>
    <el-form-item label="UserAgent过滤黑名单">
      <el-input v-model="form.UserAgentBlack" type="textarea" :rows="4"/>
    </el-form-item>
    <div class="text-block">
      <p>说明：多个UserAgent正则以换行分割，黑名单直接拦截。</p>
      <p>配置正则示例：(.*表示包含所有)</p>
      <p>.*</p>
      <p>Wget|curl|python-requests|Java|libwww-perl|HTTPClient|Urllib|PycURL|Go-http-client</p>
    </div>

    <el-divider content-position="center">区域IP过滤</el-divider>
    <el-form-item label="区域IP过滤开关">
      <el-switch v-model="form.AreaFilterOFF"
                 inline-prompt
                 active-text="开启"
                 inactive-text="关闭"/>
    </el-form-item>
    <el-form-item label="区域过滤白名单">
      <el-input v-model="form.AreaWhite" type="textarea" :rows="4"/>
    </el-form-item>
    <el-form-item label="区域过滤黑名单">
      <el-input v-model="form.AreaBlack" type="textarea" :rows="4"/>
    </el-form-item>
    <div  class="text-block">
      <p>说明：多个区域正则以换行分割，黑名单区域直接拦截。</p>
      <p>根据IP查询归属地返回的格式：国家|区域|省份|城市|ISP</p>
      <p>配置正则示例：(.*表示包含所有)</p>
      <p>.*</p>
      <p>内网</p>
      <p>中国</p>
      <p>美国</p>
      <p>北京|上海|广州</p>
      <p>昆明|曲靖</p>
    </div>

    <el-divider content-position="center">CC防护</el-divider>
    <el-form-item label="CC防护开关">
      <el-switch v-model="form.CCFilterOFF"
                 inline-prompt
                 active-text="开启"
                 inactive-text="关闭"/>
    </el-form-item>
    <el-form-item label="CC防护周期（秒）">
      <el-input-number
          v-model="form.CCFilterCycle"
          :min="1"
          controls-position="right"
      />
    </el-form-item>

    <el-form-item label="CC防护频率（次）">
      <el-input-number
          v-model="form.CCFilterFrequency"
          :min="1"
          controls-position="right"
      />
    </el-form-item>
    <el-form-item label="CC封锁时间（秒）">
      <el-input-number
          v-model="form.CCFilterSeal"
          :min="0"
          controls-position="right"
      />
    </el-form-item>
    <div class="text-block">
      <p>说明：“防护周期”秒内累计请求同一IP超过“防护频率”次，触发CC防御封锁此IP"封锁时间"秒。</p>
    </div>
    <el-form-item>
      <el-button type="primary" @click="save">保 存</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup lang="ts">
import { ElMessage , ElLoading} from 'element-plus'
import { ref, onMounted } from 'vue'
import {
  getFilterConfig,
  updateFilterConfig
} from "/@/api/system/ipfilter/filterConfig";
const form = ref({
  enableLog:false,
  IPFilterOpen:false,
  IPFilterOFF:false,
  IPWhite:"",
  IPBlack:"",
  URLFilterOFF:false,
  URLWhite:"",
  URLBlack:"",
  UserAgentFilterOFF:false,
  UserAgentWhite:"",
  UserAgentBlack:"",
  AreaFilterOFF:false,
  AreaWhite:"",
  AreaBlack:"",
  CCFilterOFF:false,
  CCFilterCycle:1,
  CCFilterFrequency:1,
  CCFilterSeal:0
})

const joinArr = (arr:string[])=> {
  if (!Array.isArray(arr) || arr.length === 0 ) return ""
  return arr.join("\n")
}


const split = (value:any) => {
  if (typeof value === "string") {
    value = value.trim()
    if (value === "") {
      return undefined
    }
    return value.split("\n")
  } else {
    return undefined
  }
}

const save = ()=> {
  //console.log(form.value)
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  updateFilterConfig({
    "enableLog": form.value.enableLog,
    "open": form.value.IPFilterOpen,
    "area": {
      "black": split(form.value.AreaBlack),
      "open": form.value.AreaFilterOFF,
      "white": split(form.value.AreaWhite)
    },
    "cc": {
      "cycle": form.value.CCFilterCycle,
      "frequency": form.value.CCFilterFrequency,
      "open": form.value.CCFilterOFF,
      "seal": form.value.CCFilterSeal
    },
    "ip": {
      "black": split(form.value.IPBlack),
      "open":  form.value.IPFilterOFF,
      "white": split(form.value.IPWhite)
    },
    "url": {
      "black":  split(form.value.URLBlack),
      "open": form.value.URLFilterOFF,
      "white":  split(form.value.URLWhite)
    },
    "userAgent": {
      "black": split(form.value.UserAgentBlack),
      "open": form.value.UserAgentFilterOFF,
      "white":  split(form.value.UserAgentWhite)
    }
  }).then((res:any)=> {
    const {code, message} = res
    if (code === 0) {
      ElMessage.success("保存成功")
    } else {
      ElMessage.error(message || "保存失败")
    }

  }).finally(()=> {
    loading.close()
  })
}

onMounted(()=> {
  getFilterConfig().then((res:any)=> {
    //console.log(res)
    const {code, data, message} = res
    if (code === 0) {
      const config = data.config
      form.value.enableLog = config.enableLog
      form.value.IPFilterOpen = config.open
      form.value.IPFilterOFF = config.ip.open
      form.value.IPWhite = joinArr(config.ip.white)
      form.value.IPBlack = joinArr(config.ip.black)

      form.value.URLFilterOFF = config.url.open
      form.value.URLWhite = joinArr(config.url.white)
      form.value.URLBlack = joinArr(config.url.black)

      form.value.UserAgentFilterOFF = config.userAgent.open
      form.value.UserAgentWhite = joinArr(config.userAgent.white)
      form.value.UserAgentBlack = joinArr(config.userAgent.black)

      form.value.AreaFilterOFF = config.area.open
      form.value.AreaWhite = joinArr(config.area.white)
      form.value.AreaBlack = joinArr(config.area.black)

      form.value.CCFilterOFF = config.cc.open
      form.value.CCFilterCycle = config.cc.cycle
      form.value.CCFilterFrequency = config.cc.frequency
      form.value.CCFilterSeal = config.cc.seal
    } else {
      ElMessage.error(message || "配置信息获取错误")
    }
  })
})
</script>
<style scoped lang="scss">
.text-block{
  margin-bottom: 10px;
  padding: 10px;
  background-color: #c8c9cc;

  border-radius: 4px;
  p{
    margin: 0;
    color: #fff;
    font-size: 14px;
    line-height: 1.5;
  }
}
.filter-open {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 40px;
  background-color: #f5f7fa;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  font-size: 14px;
  line-height: 40px;
  text-align: center;
  width:200px;
}
</style>